package de.wwi11b2.mjp.comicvineclient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

public class DatabaseActivity extends Activity {

	private ExpandableListView databaseContentExpandableListView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_database);
		databaseContentExpandableListView = (ExpandableListView) findViewById(R.id.databaseContentExpandableListView);
		databaseContentExpandableListView
				.setOnChildClickListener(new OnChildClickListener() {

					@Override
					public boolean onChildClick(ExpandableListView parent,
							View v, int groupPosition, int childPosition,
							long id) {
						Item item = ((DatabaseExpandableListAdapter) databaseContentExpandableListView
								.getExpandableListAdapter()).getChild(groupPosition,
								childPosition);
						Intent intent = new Intent();
						intent.setClass(getApplicationContext(), ItemActivity.class);
						intent.putExtra("item", item);
						intent.putExtra("isFromSearch", false);
						startActivity(intent);
						return false;
					}
				});
	}

	@Override
	protected void onResume() {
		super.onResume();
		ArrayList<String> listDataHeader = new ArrayList<String>();
		listDataHeader.add("Chatacters");
		listDataHeader.add("Locations");
		HashMap<String, List<Item>> listDataChild = new HashMap<String, List<Item>>();
		DataHelper dataHelper = new DataHelper(getApplicationContext());
		listDataChild.put(listDataHeader.get(0),
				dataHelper.findAll(Item.ResourceType.CHARACTER.ordinal()));
		listDataChild.put(listDataHeader.get(1),
				dataHelper.findAll(Item.ResourceType.LOCATION.ordinal()));
		databaseContentExpandableListView
				.setAdapter(new DatabaseExpandableListAdapter(
						getApplicationContext(), listDataHeader, listDataChild));
	}

}
